import Vue from 'vue';
import store from './store'
Vue.config.errorHandler = function(err, vm, info) {

    Vue.nextTick(() => {
        store.commit('ADD_LOGS', {
            type: 'error',
            message: err.message,
            stack: err.stack,
            info
        })
        if (process.env.NODE_ENV === 'development') {
            console.group('>>>>>> 错误信息 >>>>>>')
            console.log(info)
            console.groupEnd();
            console.group('>>>>>> Vue 实例 >>>>>>')
            console.log(vm)
            console.groupEnd();
            console.group('>>>>>> Error >>>>>>')
            console.log(err)
            console.groupEnd();
        }
    })
}

/**
 * 生成rule
 * @param validatorName 验证名字
 * @param trigger 类型 blur || change
 * @param required 是否必填
 * @returns {{trigger: *, message: string, required: *}}
 */
 Vue.prototype.$ruler = (validatorName, trigger = 'blur', required = true) => {
    return { message: `请${trigger === 'blur' ? '输入' : '选择'}${validatorName}`, trigger, required }
}

  Vue.prototype.msgSuccess = function (msg) {
    this.$message({ showClose: true, message: msg, type: "success" });
  }
  
  Vue.prototype.msgError = function (msg) {
    this.$message({ showClose: true, message: msg, type: "error" });
  }
  
  Vue.prototype.msgInfo = function (msg) {
    this.$message.info(msg);
  }
  
  // 确认窗体
  Vue.prototype.confirm = function (content) {
    return this.$confirm(content, "系统提示", {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: "warning",
    })
  }

  // 通用下载方法
Vue.prototype.download = function(fileName) {
	window.location.href = process.env.VUE_APP_BASE_API + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
}

// 表单重置
Vue.prototype.resetForm = function(refName) {
	if (this.$refs[refName]) {
		this.$refs[refName].resetFields();
	}
}